#include <NewSoftSerial.h>
#include <uMMC.h>

NewSoftSerial ummc_s(4, 5);

static int8_t availableummc(void);
static int peekummc(void);
static int readfromummc(void);
static void printtoummc(char c);

uMMC ummc1(&availableummc, &peekummc, &readfromummc, &printtoummc);

static void printtoummc(uint8_t c)
{
  ummc_s.print((uint8_t) c);
}

static int readfromummc(void)
{
  return ummc_s.read();
}

static int8_t availableummc(void)
{
  return ummc_s.available();
}

static int peekummc(void)
{
  return ummc_s.peek();
}

void setup()
{
  pinMode(13, OUTPUT);

  Serial.begin(9600);

  ummc_s.begin(9600);
}

void loop()
{
  int8_t resp, filehandle;
  char filename[64];

  Serial.println("Initializing uMMC");

  // prepares the communications with the uMMC and closes all open files (if any)
  ummc1.sync();
  
  Serial.print("uMMC Version: ");
  Serial.println(ummc1.version());

  // list all files

  Serial.print("File count: ");
  
  Serial.println(ummc1.filecount("/", "*"), DEC);

  Serial.println("--- Files ---");

  ummc1.opendir("/");

  while(ummc1.readdir(filename, "*") == 0)
  {
    Serial.println(filename);
  }

  Serial.println("-------------");

  while(1);  // just loop infinitely here at the end, wait for reset
}
